#include <cjson/cJSON.h>

#include "../util.h"
#include "pageinfo.h"

action_pageinfo_result action_pageinfo(
    const char* bind_interface,
    const char* portal_host,
    const char* portal_search
) {
    action_pageinfo_result result = {
        ACTION_FETCHED_SUCCESSFULLY,
        NULL,
        true,
        NULL,
        NULL
    };
    
    const char* addr_strs[] = {
        "http://",
        portal_host,
        "/eportal/InterFace.do?method=pageInfo",
        NULL
    };
    char* address = string_concat(addr_strs);
    
    const char* post_strs[] = {
        "queryString=",
        portal_search == NULL ? NULL : url_encode_component(portal_search),
        NULL
    };
    char* post_string = string_concat(post_strs);
    if (post_strs[1] != NULL) {
        free((char*) post_strs[1]);
    }
    
    CURL_EASY_SETUP({
        // setup action
        curl_easy_setopt(curl, CURLOPT_COPYPOSTFIELDS, post_string);
        curl_common_setup(curl, portal_host, portal_search);
        free(post_string);
    }, {
        cJSON* pageinfo = cJSON_Parse(chunk.memory);
        if (pageinfo == NULL) {
            goto failure;
        }
        const cJSON* loginText;
        const cJSON* passwordEncrypt;
        const cJSON* publicKeyExponent;
        const cJSON* publicKeyModulus;
        
        loginText = cJSON_GetObjectItemCaseSensitive(pageinfo, "loginText");
        if (cJSON_IsString(loginText) && (loginText -> valuestring != NULL)) {
            result.loginText = string_duplicate(loginText -> valuestring);
        } else {
            goto failure;
        }
        
        passwordEncrypt = cJSON_GetObjectItemCaseSensitive(
            pageinfo, "passwordEncrypt");
        if (cJSON_IsString(passwordEncrypt) &&
            (passwordEncrypt -> valuestring != NULL)) {
            if (strcmp(passwordEncrypt -> valuestring, "true") == 0) {
                result.passwordEncrypt = true;
            } else if (strcmp(passwordEncrypt -> valuestring, "false") == 0) {
                result.passwordEncrypt = false;
            } else {
                goto failure;
            }
        } else {
            goto failure;
        }
        
        publicKeyExponent = cJSON_GetObjectItemCaseSensitive(
            pageinfo, "publicKeyExponent");
        if (cJSON_IsString(publicKeyExponent) &&
            (publicKeyExponent -> valuestring != NULL)) {
            result.publicKeyExponent =
                string_duplicate(publicKeyExponent -> valuestring);
        } else {
            goto failure;
        }
        
        publicKeyModulus = cJSON_GetObjectItemCaseSensitive(
            pageinfo, "publicKeyModulus");
        if (cJSON_IsString(publicKeyModulus) &&
            (publicKeyModulus -> valuestring != NULL)) {
            result.publicKeyModulus =
                string_duplicate(publicKeyModulus -> valuestring);
        } else {
            goto failure;
        }
        
        end:
        cJSON_free(pageinfo);
        free(address);
        return result;
        
        failure:
        result.status = ACTION_PAGEINFO_PARSE_FAILURE;
        goto end;
    }, {
        // failure action
        result.status = ACTION_FETCHING_FAILURE;
        free(address);
        return result;
    }, address, bind_interface, 8192);
}
